package com.andyou.rest.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.andyou.common.persistence.Page;
import com.andyou.common.service.BaseService;
import com.andyou.common.service.CrudService;
import com.andyou.modules.sys.entity.User;
import com.andyou.modules.sys.utils.UserUtils;
import com.andyou.rest.dao.AppInfoDao;
import com.andyou.rest.entity.AppInfo;

/**
 * 接口接入管理Service
 * @author lgx
 * @version 2017-08-22
 */
@Service
@Transactional(readOnly = true)
public class AppInfoService extends CrudService<AppInfoDao, AppInfo> {

	public AppInfo get(String id) {
		return super.get(id);
	}
	
	public List<AppInfo> findList(AppInfo appInfo) {
		return super.findList(appInfo);
	}
	
	public Page<AppInfo> findPage(Page<AppInfo> page, AppInfo appInfo) {
		User user = UserUtils.getUser();
		// 不是超级管理员
		if (!user.isAdmin()) {
			appInfo.getSqlMap().put("dsf", BaseService.dataScopeFilter(user, "o", "u"));

			// 属于同一个公司的都可以看到数据
			String sqlWhere = " a.company_id = '#companyId#' ";
			sqlWhere = sqlWhere.replace("#companyId#", user.getCompany().getId());
			super.dataScopeFilterByUserDefined(appInfo, "dataFilterByCompany", sqlWhere,
					false);
		}
		appInfo.setPage(page);
		page.setList(dao.findList(appInfo));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public int save(AppInfo appInfo) {
		appInfo.setCompanyId(UserUtils.getUser().getCompany().getId());
		appInfo.setCompanyName(UserUtils.getUser().getCompany().getName());
		
		return super.save(appInfo);
	}
	
	@Transactional(readOnly = false)
	public void delete(AppInfo appInfo) {
		super.delete(appInfo);
	}
	
	
    @Transactional(readOnly = false)
	public void batchDelete(List<String> idList) {
		AppInfo appInfo = null;
		for (String id : idList) {
			appInfo = new AppInfo();
			appInfo.setId(id);
			super.delete(appInfo);
		}
	}
}